package com.sf.wc.filereader;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import com.sf.wc.constant.Constant;


/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年12月5日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class TxtFileReader {
	private static final String WORD_COUNT_PATH = Constant.WORD_COUNT_PATH;
	private static final Logger logger = LoggerFactory.getLogger(TxtFileReader.class);
	private TxtFileReader() {
		
	}
	public Stream<String> read() {
		try {
			ClassPathResource classPathResource = new ClassPathResource(WORD_COUNT_PATH);
			return Files.lines(Paths.get(classPathResource.getURI()), StandardCharsets.UTF_8);
			
		} catch (IOException e) {
			logger.error("read wordcount.txt fail",e);
		}
		return null;
	}
	private static class Singleton {
		private static TxtFileReader fileReader;
		private Singleton() {
			
		}
		static {
			fileReader = new TxtFileReader();
		}
		public static TxtFileReader getInstance() {
			return fileReader;
		}
	}
	public static TxtFileReader getInstance() {
		return Singleton.getInstance();
	}
}
